//
//  TheOrderDetailsViewController.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/15.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "TheOrderDetailsViewController.h"
#import "TheOrderDetailsTableViewCell.h"
#import "MakeSureTheOrderOneTableViewCell.h"
#import "TheOrderDetailsTimeTableViewCell.h"

@interface TheOrderDetailsViewController ()<UITableViewDelegate , UITableViewDataSource>
{
    NSDictionary *model;
}

Strong UIView *headView;
Strong UIView *footView;
Strong UILabel *phoneNumberLabel;
Strong UITableView *tableView;
Strong NSMutableArray *dataArray; /**<*/
Strong UILabel *moneyLabel;
Strong UILabel *theOrderNumber;
Strong UILabel *theOrderNumberLabel;
Strong UILabel *logisticsInformationLabel; // 物流信息
Strong UIButton *toApplyForARefundButton; //申请退款
Strong UIButton *thumbUpButton; /**<点赞按钮*/
Strong NSMutableArray *contentArray; /**<*/
Strong NSMutableArray *timeArray; /**<*/

@end

//网络
@implementation TheOrderDetailsViewController (NetWorking)

- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"soId":self.so_id};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Store_Order/viewOrderDetails.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            model = responseObject[@"obj"];
            self.contentArray = responseObject[@"obj2"];
            [self.dataArray addObjectsFromArray:responseObject[@"obj1"]];
//            [self viewOrderLogisticsInformation];
            [self.view addSubview:self.tableView];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
//
//- (void)viewOrderLogisticsInformation{
//    [self showprogressHUD];
//    NSDictionary *parameters = @{@"":@"",
//                                 @"":@""};
//    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Store_Order/viewOrderLogisticsInformation.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        [self hiddenProgressHUD];
//        if ([responseObject[@"responseState"] isEqualToString:@"0"]) {
//            [self.view makeToast:responseObject[@"msg"] duration:1 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
//                [self.navigationController popViewControllerAnimated:YES];
//            }];
//        }else{
//            ShowMessage(responseObject[@"msg"]);
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [self hiddenProgressHUD];
//        [self showError:error];
//    }];
//}

- (void)evaluateOrder{
    NSDictionary *parameters = @{@"soId":self.so_id};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Store_Order/evaluateOrder.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}

@end

@implementation TheOrderDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - click

- (void)setSo_id:(NSString *)so_id{
    _so_id = so_id;
    [self getData];
}

- (void)thumbUpButtonClick:(UIButton *)sender{
    if (!sender.selected) {
        [self evaluateOrder];
    }
    sender.selected = YES;
}

- (void)toApplyForARefundButtonClick{
    NSLog(@"申请退款");
}

- (void)bgViewClick{
    [self callPhone:self.phoneNumberLabel.text];
}

#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.contentArray.count;
    }else if(section == 1){
//        return self.timeArray.count;
        return 1;
    }
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        MakeSureTheOrderOneTableViewCell *cell = [MakeSureTheOrderOneTableViewCell cellForTableView:tableView];
        cell.model = self.contentArray[indexPath.row];
    
        return cell;
    }else if (indexPath.section == 1){
        TheOrderDetailsTimeTableViewCell *cell = [TheOrderDetailsTimeTableViewCell cellForTableView:tableView];
        cell.model = model;
        return cell;
    }
    TheOrderDetailsTableViewCell *cell = [TheOrderDetailsTableViewCell cellForTableView:tableView];
    cell.indexPath = indexPath;
    cell.dataArray = self.dataArray;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (![self.dataArray[indexPath.row][@"lc_mobile"] isEqualToString:@" "]) {
        [ self callPhone:self.dataArray[indexPath.row][@"phone"] ];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            return 65 / WIDTH_5S_SCALE;
        case 1:
            return 60 / WIDTH_5S_SCALE;

        case 2:
            return 60 / WIDTH_5S_SCALE;

    }
    return 60 / WIDTH_5S_SCALE;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else if(section == 1){
        return 66 / WIDTH_5S_SCALE + 0.5;
    }
    if (self.dataArray.count == 0) {
        return 0;
    }
    return 33 / WIDTH_5S_SCALE + 1;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    if (section == 1) {
        view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 66 / WIDTH_5S_SCALE + 0.5);
        UILabel *allMoneyLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 0, 200 / WIDTH_5S_SCALE, 33 / WIDTH_5S_SCALE)];
        allMoneyLabel.font = DEF_FontSize_14;
        allMoneyLabel.textColor = getColor(mainColor);
        allMoneyLabel.text = @"总价";
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 33 / WIDTH_5S_SCALE, SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = getColor(dividerColor);
        
        [view sd_addSubviews:@[allMoneyLabel , self.moneyLabel , lineView , self.theOrderNumber , self.theOrderNumberLabel]];
    }else{
        if (self.dataArray.count == 0) {
            return view;
        }
        view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 33 / WIDTH_5S_SCALE + 1);
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 33 / WIDTH_5S_SCALE + 0.5, SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = getColor(dividerColor);
        UIView *line1View = [[UIView alloc]initWithFrame:CGRectMake(0, 0.5, SCREEN_WIDTH, 0.5)];
        line1View.backgroundColor = getColor(dividerColor);
        [view sd_addSubviews:@[self.logisticsInformationLabel , lineView , line1View]];
    }
    
    return view;
}

#pragma mark - init

- (UIButton *)thumbUpButton{
    if (!_thumbUpButton) {
        _thumbUpButton = [[UIButton alloc]init];
        [_thumbUpButton setImage:[UIImage imageNamed:@"thumbUp_nor"] forState:UIControlStateNormal];
        [_thumbUpButton addTarget:self action:@selector(thumbUpButtonClick:) forControlEvents:UIControlEventTouchUpInside];
         [self.thumbUpButton setImage:[UIImage imageNamed:@"thumbUp_sel"] forState:UIControlStateSelected];
        if (self.buttonType) {
            self.thumbUpButton.selected = YES;
        }
    }
    return _thumbUpButton;
}

- (UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150 / WIDTH_5S_SCALE)];
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = getColor(dividerColor);
        
        UIView *line1View = [[UIView alloc]init];
        line1View.backgroundColor = getColor(dividerColor);
        
        UIView *line2View = [[UIView alloc]init];
        line2View.backgroundColor = getColor(dividerColor);
        
        UILabel *leftLabel  = [[UILabel alloc]init];
        leftLabel.font = DEF_FontSize_14;
        leftLabel.textColor = getColor(mainColor);
        
        NSMutableAttributedString *attStr1 = [NSMutableAttributedString new];
        NSTextAttachment *seeAtt1 = [NSTextAttachment new];
        seeAtt1.bounds = CGRectMake(0, - 2, 12 / WIDTH_5S_SCALE, 12 / WIDTH_5S_SCALE);
        seeAtt1.image = [UIImage imageNamed:@"left_image"];
        [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt1]];
        [attStr1 appendAttributedString:[[NSAttributedString alloc] initWithString:@" 点赞好评"]];
        leftLabel.attributedText = attStr1;
        
        UILabel *titleLabel  = [[UILabel alloc]init];
        titleLabel.font = DEF_FontSize_14;
        titleLabel.textColor = getColor(@"999999");
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = @"如果您对我们的服务非常满意,请不要忘记点赞哦!";
        
        [_footView sd_addSubviews:@[lineView , line1View , line2View , leftLabel , titleLabel , self.toApplyForARefundButton , self.thumbUpButton]];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(0 / WIDTH_5S_SCALE);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 0.5));
        }];
        
        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
            make.top.equalTo(lineView.mas_bottom).offset(0);
            make.height.mas_equalTo(30 / WIDTH_5S_SCALE);
        }];
        
        [self.thumbUpButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(- 10 / WIDTH_5S_SCALE);
            make.top.equalTo(lineView.mas_bottom).offset(3 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(24 / WIDTH_5S_SCALE);
            make.width.mas_equalTo(24 / WIDTH_5S_SCALE);
        }];
        
        [line1View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.equalTo(self.thumbUpButton.mas_bottom).offset(0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 0.5));
        }];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(0);
            make.top.equalTo(line1View.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(45 / WIDTH_5S_SCALE);
            make.left.mas_equalTo(0);
        }];
        
        [line2View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.equalTo(titleLabel.mas_bottom).offset(0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 0.5));
        }];
        
        [self.toApplyForARefundButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo((SCREEN_WIDTH - 105 / WIDTH_5S_SCALE) / 2);
            make.top.equalTo(line2View.mas_bottom).offset(26 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(21 / WIDTH_5S_SCALE);
            make.width.mas_equalTo(105 / WIDTH_5S_SCALE);
        }];
    }
    return _footView;
}

- (UIButton *)toApplyForARefundButton{
    if (!_toApplyForARefundButton) {
        _toApplyForARefundButton = [[UIButton alloc]init];
        _toApplyForARefundButton.titleLabel.font = DEF_FontSize_14;
        _toApplyForARefundButton.backgroundColor = getColor(mainColor);
        _toApplyForARefundButton.layer.masksToBounds = YES;
        _toApplyForARefundButton.layer.cornerRadius = 21 / WIDTH_5S_SCALE / 2;
        [_toApplyForARefundButton setTitle:@"申请退款" forState:UIControlStateNormal];
        [_toApplyForARefundButton setTitleColor:getColor(@"ffffff") forState:UIControlStateNormal];
        [_toApplyForARefundButton addTarget:self action:@selector(toApplyForARefundButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _toApplyForARefundButton;
}

- (UILabel *)theOrderNumberLabel{
    if (!_theOrderNumberLabel) {
        _theOrderNumberLabel  = [[UILabel alloc]initWithFrame:CGRectMake(0, 33 / WIDTH_5S_SCALE + 0.5, SCREEN_WIDTH - 10 / WIDTH_5S_SCALE, 33 / WIDTH_5S_SCALE)];
        _theOrderNumberLabel.font = DEF_FontSize_14;
        _theOrderNumberLabel.textColor = getColor(textColor);
        _theOrderNumberLabel.textAlignment = NSTextAlignmentRight;
        _theOrderNumberLabel.text = model[@"so_number"];
    }
    return _theOrderNumberLabel;
}

- (UILabel *)logisticsInformationLabel{
    if (!_logisticsInformationLabel) {
        _logisticsInformationLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 0.5, SCREEN_WIDTH, 33 / WIDTH_5S_SCALE)];
        _logisticsInformationLabel.font = DEF_FontSize_14;
        _logisticsInformationLabel.textColor = getColor(textColor);
        _logisticsInformationLabel.attributedText = [self getAttributeName];
    }
    return _logisticsInformationLabel;
}

- (NSAttributedString *)getAttributeName{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
//    NSAttributedString *place = [[NSAttributedString alloc]initWithString:@"(本服务由XXX物流公司提供)" attributes:@{NSFontAttributeName:DEF_FontSize_12}];
    NSTextAttachment *seeAtt = [NSTextAttachment new];
    seeAtt.bounds = CGRectMake(0, -2 / WIDTH_5S_SCALE, 10 / WIDTH_5S_SCALE, 13 / WIDTH_5S_SCALE);
    seeAtt.image = [UIImage imageNamed:@"ShopIsIntroduced"];
    [attStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",@"物流信息"]]];
//    [attStr appendAttributedString:place];
    return attStr;
}

- (UILabel *)phoneNumberLabel{
    if (!_phoneNumberLabel) {
        _phoneNumberLabel  = [[UILabel alloc]initWithFrame:CGRectMake(100 / WIDTH_5S_SCALE, 0, SCREEN_WIDTH - 110 / WIDTH_5S_SCALE, 28 / WIDTH_5S_SCALE)];
        _phoneNumberLabel.font = DEF_FontSize_14;
        _phoneNumberLabel.textColor = getColor(mainColor);
        _phoneNumberLabel.textAlignment = NSTextAlignmentRight;
        _phoneNumberLabel.text = model[@"s_mobile"];
    }
    return _phoneNumberLabel;
}

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 28 / WIDTH_5S_SCALE)];
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 28 / WIDTH_5S_SCALE)];
        
        UILabel *leftLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 0, 200, 28 / WIDTH_5S_SCALE)];
        leftLabel.font = DEF_FontSize_14;
        leftLabel.textColor = getColor(mainColor);
        NSMutableAttributedString *attStr1 = [NSMutableAttributedString new];
        NSTextAttachment *seeAtt1 = [NSTextAttachment new];
        seeAtt1.bounds = CGRectMake(0, - 2, 10 / WIDTH_5S_SCALE, 12 / WIDTH_5S_SCALE);
        seeAtt1.image = [UIImage imageNamed:@"phone"];
        [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt1]];
        [attStr1 appendAttributedString:[[NSAttributedString alloc] initWithString:@" 联系商家"]];
        leftLabel.attributedText = attStr1;
        [bgView addSubview:leftLabel];
        [bgView addSubview:self.phoneNumberLabel];
        
        bgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgViewClick)];
        [bgView addGestureRecognizer:tap];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 28 / WIDTH_5S_SCALE, SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = getColor(dividerColor);
        [_headView addSubview:lineView];
        
        UIView *line1View = [[UIView alloc]init];
        line1View.backgroundColor = getColor(dividerColor);
        
        [_headView sd_addSubviews:@[line1View , bgView]];

        
    }
    return _headView;
}

- (UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel  = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 10 / WIDTH_5S_SCALE, 33 / WIDTH_5S_SCALE)];
        _moneyLabel.font = DEF_FontSize_14;
        _moneyLabel.textColor = getColor(mainColor);
        _moneyLabel.textAlignment = NSTextAlignmentRight;
         _moneyLabel.text = [NSString stringWithFormat:@"¥%.2f",[model[@"so_total"] floatValue]];
    }
    return _moneyLabel;
}

- (UILabel *)theOrderNumber{
    if (!_theOrderNumber) {
        _theOrderNumber  = [[UILabel alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 33 / WIDTH_5S_SCALE + 0.5, SCREEN_WIDTH, 33 / WIDTH_5S_SCALE)];
        _theOrderNumber.font = DEF_FontSize_14;
        _theOrderNumber.textColor = getColor(textColor);
        NSMutableAttributedString *attStr1 = [NSMutableAttributedString new];
        NSTextAttachment *seeAtt1 = [NSTextAttachment new];
        seeAtt1.bounds = CGRectMake(0, -2 / WIDTH_5S_SCALE, 10 / WIDTH_5S_SCALE, 13 / WIDTH_5S_SCALE);
        seeAtt1.image = [UIImage imageNamed:@"root_TheOrderNumber"];
        [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt1]];
        [attStr1 appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",@"订单号"]]];
        _theOrderNumber.attributedText = attStr1;
    }
    return _theOrderNumber;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footView;
        _tableView.tableHeaderView = self.headView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
    }
    return _tableView;
}


#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[
                        ].mutableCopy;
    }
    return _dataArray;
}

- (NSMutableArray *)contentArray{
    if (!_contentArray) {
        _contentArray  = @[
                        ].mutableCopy;
    }
    return _contentArray;
}

- (NSMutableArray *)timeArray{
    if (!_timeArray) {
        _timeArray  = @[
                           ].mutableCopy;
    }
    return _timeArray;
}



@end
